package com.keeplearning;

import com.google.common.collect.Lists;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class DiamonService {
    public static List<DiamondRow> create(char c) {
        short distant = distant(c);

        // upper
        List<DiamondRow> upperRows = IntStream.range(0, distant)
                .mapToObj(i -> DiamondRow.builder()
                        .leftPad(distant - i)
                        .middlePad(i*2)
                        .letter((char) (c - distant + i))
                        .build())
        .collect(Collectors.toList());


        // middle

        DiamondRow middleRow = DiamondRow.builder()
                .middlePad(distant * 2)
                .leftPad(0)
                .letter(c)
                .build();


        // down

        List<DiamondRow> downRows = Lists.reverse(upperRows);


        List<DiamondRow> all = new ArrayList<>(upperRows);
        all.add(middleRow);
        all.addAll(downRows);
        return all;
    }

    public  static void  print(List<DiamondRow> rows) {
        rows
                .forEach(row ->  System.out.println(row.format()));

    }


    public static short distant(char c) {
        if (Character.isLetter(c)) {
            return (short) (c - (Character.isUpperCase(c) ? 'A' : 'a'));
        } else {
            throw new IllegalArgumentException("argument must letter");
        }


    }

}
